package com.mission.shaco.warehouse

import android.os.Bundle
import android.text.TextUtils
import android.text.method.ScrollingMovementMethod
import com.androidquery.AQuery
import com.androidquery.callback.AjaxCallback
import com.androidquery.callback.AjaxStatus
import com.mission.shaco.warehouse.models.ClientConfig
import kotlinx.android.synthetic.main.activity_recive.*
import org.json.JSONObject

class ReciveActivity : ScannerActivity() {

    val trackNumbers : JSONObject  = JSONObject()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_recive)
        initView()
    }

    fun initView(){
        txtPreView.movementMethod = ScrollingMovementMethod()
    }

    /**
     *  配货匹配成功提示
     */
    fun successNotice(){
        player.start()
        vibrator.vibrate(800)
    }

    /**
     *  配货匹配失败提示
     */
    fun failedNotice(){
        vibrator.vibrate(300)
    }

    override fun afterScanReceive() {

        if(!isCurrentClass()) return
        if(!TextUtils.isEmpty(code)) {
            if(!trackNumbers.has(code)) {
                successNotice()
                if(!trackNumbers.has(code)){
                    trackNumbers.put(code,1)
                    var t = Thread(searchThread)
                    t.start()
                    txtPreView.text = code + "\r\n"  + txtPreView.text
                }
            }
        }
    }


    var searchThread: Runnable = Runnable {
        try {
            val aq: AQuery = AQuery(this)
            val params = HashMap<String, Any>()

            var map = JSONObject()
            map.put("picker",getOpreateName())
            map.put("trackNumber", code)
            code = ""
            params.put("datas", map)
            var jsonStr = map.toString()

            aq.ajax(ClientConfig.getHostUrl(ClientConfig.JSON_TRACKING_CONFIRM),params, String::class.java,  object : AjaxCallback<String>() {

                override fun callback(url: String, result: String, status: AjaxStatus) {
                    if(status.code == 200){
                        var response: JSONObject = JSONObject(result)
                        if(response.getInt("status") == 1) {
                        }else{
                            noticeMessage(response.getString("msg"))
                        }
                    }else{
                        noticeMessage("网络出问题")
                    }
                }
            })

        } catch (e: InterruptedException) {
            e.printStackTrace()
        }
    }


}
